﻿using UnityEngine;
using System.Collections;

public class StoreSceneManager : LTEventListenerC {
    enum STORECATEGORY
    {
        MAIN,
        CHARACTER,
        WEAPON,
        ITEM,
        CASH
    }

    public GameObject storeRootObject;
	public GameObject storeMainViewObject;
	public GameObject[] storeDetailViewObject;

	public UITexture storeMainADImage;
    public Texture2D[] storeMainADImages;
	public GameObject storeMainShugoObject;

    public UILabel shugoDialogBodyLabel;

    public GameObject itemUnitListObject;


    STORECATEGORY currentCategory = STORECATEGORY.MAIN;

    public override void AwakeObject()
    {
        base.AwakeObject();

        SoundController.Create();

        BGM = "BGM_Market";
    }

	// Use this for initialization
	void Start () {
        ShowShugoTipDialog();

        if( Localization.language == Language.text_CN.ToString() )
        {
            storeMainADImage.mainTexture = storeMainADImages[1];
        }

        SetActiveHelpButton(false);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);

        SoundController.PlaySound("UI_store_button");

		if( eventID == 0 )	//characterStore
		{
            currentCategory = STORECATEGORY.CHARACTER;
			OpenCharacterItemStoreView();
		}
		else if( eventID == 1 )		//equipItemStore
		{
            ToastUtility.UnavailableToast();
            currentCategory = STORECATEGORY.WEAPON;
		}
		else if( eventID == 2 )		//ItemStore	
		{
            ToastUtility.UnavailableToast();
            currentCategory = STORECATEGORY.ITEM;
		}
		else if( eventID == 3 )		//CashStore
		{
            ToastUtility.UnavailableToast();
            currentCategory = STORECATEGORY.CASH;
		}
	}

	public void OpenCharacterItemStoreView()
	{
        //storeMainViewObject.SetActive(false);
        //storeDetailViewObject[0].SetActive(true);
        storeRootObject.GetComponent<Animation>().Play("Move_char");

        Invoke("ShowCharacterItemUnitList", 0.66f);
	}

    void ShowCharacterItemUnitList()
    {
        itemUnitListObject.SetActive(true);
    }

    public override void BackButton()
    {
        base.BackButton();
        Debug.LogError(currentCategory);
        if (currentCategory == STORECATEGORY.CHARACTER)
        {
            if (!CharacterDrawViewManager.instance.characterPreviewViewObject.transform.FindChild("Root").gameObject.activeSelf)
            {
                //CharacterDrawViewManager.instance.ResetCardFlipState();
                CharacterDrawViewManager.instance.tipShugoObject.SetActive(false);
                storeRootObject.GetComponent<Animation>().Play("Back_char");
                //itemUnitListObject.SetActive(false);
                currentCategory = STORECATEGORY.MAIN;
            }
            else
            {
                CharacterDrawViewManager.instance.characterPreviewViewObject.GetComponent<Animation>().Play("RootAlpha_Out"); 
            }
        }
        else if (currentCategory == STORECATEGORY.MAIN)
        {
            base.HomeButton();
        }
    }

    bool isfirstTimeToShowShugoTipDialog = false;
    int tipBodyIndex = 1;
    public void ShowShugoTipDialog()
    {
        //팁 텍스트를 세팅
        if (!isfirstTimeToShowShugoTipDialog)
        {
            shugoDialogBodyLabel.text = TextController.GetStringWithId("StoreShugoTextmain");
            isfirstTimeToShowShugoTipDialog = true;
            SoundController.PlaySound("store_shugo_welcome");
        }
        else
        {
            if (tipBodyIndex > 4)
                tipBodyIndex = 1;
            shugoDialogBodyLabel.text = TextController.GetStringWithId(string.Format("StoreShugoText{0}", tipBodyIndex++));
            if (tipBodyIndex == 1)
                SoundController.PlaySound("store_shugo_sale");
            else if (tipBodyIndex == 2)
                SoundController.PlaySound("store_shugo_charming");
            else if (tipBodyIndex == 3)
                SoundController.PlaySound("store_shugo_thanks");
            else if (tipBodyIndex == 4)
                SoundController.PlaySound("store_shugo_pain");
        }
    }
}
